package NC;

public class NC10 {
    public boolean isBalanced(TreeNode root) {
        return dfs(root)>=0;
    }

    // 返回值表示以该节点为跟节点的深度
    int dfs(TreeNode root) {
        if(root==null) return 0;
        int left=dfs(root.left),right=dfs(root.right);
        if(left>=0&&right>=0&&Math.abs(left-right)<=1) return 1+Math.max(left,right);
        else return -1;
    }
}
